﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

namespace LabMS.Web.BaseData
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class SaveCource : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string strJson = System.Web.HttpContext.Current.Server.UrlDecode(context.Request.Form.ToString());
                
                LabMS.Model.CourseJson json = (LabMS.Model.CourseJson)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(strJson,
                    typeof(LabMS.Model.CourseJson));

                ExtendBLL.Course course = new ExtendBLL.Course();

                if (course.Exists(" Course_Code = @Code",new System.Data.SqlClient.SqlParameter("@Code",json.Code)))
                {
                    throw new Exception("课程号已经存在");
                }


                LabMS.BLL.ProCourse proCourse = new LabMS.BLL.ProCourse();
                LabMS.Model.Course courseModel = new LabMS.Model.Course();//课程
                LabMS.Model.ProCourse proCourseModel = new LabMS.Model.ProCourse();//专业课程

                courseModel.Course_Code = json.Code;
                courseModel.Course_ExprientHouse = json.Hour;
                courseModel.Course_Name = json.Name;
                courseModel.CourseType = json.Type;
                courseModel.EnglishName = json.EnglishName;
                courseModel.Course_House =json.CourseHouse.ToString();//课时

                courseModel.ID = course.Add(courseModel);
                 
                proCourseModel.Cou_ID = courseModel.ID;//取出课程ID，存专业课程表

                foreach (LabMS.Model.MajorJson major in json.Majors)
                {
                    proCourseModel.ProID = major.ID;

                    proCourse.Add(proCourseModel);
                }

                context.Response.ContentType = "application/json";
                context.Response.Write("{success: true}");
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "application/json";
                context.Response.Write("{error: true, msg: '" + System.Web.HttpContext.Current.Server.UrlEncode(ex.Message) + "'}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
